import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/afterSales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询GoodWill数据'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询GoodWill数据详情'
});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增GoodWill数据'
});

const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改GoodWill数据'
});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});
const getConfirmDesc = formatMessage({
    id: 'api.getConfirmDesc',
    defaultMessage: '确认'
});
const getSubmitDesc = formatMessage({
    id: 'api.getSubmitDesc',
    defaultMessage: '提交'
});
const getVinDesc = formatMessage({
    id: 'api.getVinDesc',
    defaultMessage: '车辆售后档案查询'
});
const faultItemQueryDesc = formatMessage({
    id: 'api.faultItemQueryDesc',
    defaultMessage: '查询故障代码'
});
const getPersonnelDesc = formatMessage({
    id: 'api.getPersonnelDesc',
    defaultMessage: '查询人员信息'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/goodWill/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询GoodWill数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/goodWills?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询GoodWill数据详情
export const getDetail = id => request(`${baseUrl}/goodWills/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/goodWills`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/goodWills/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 作废
export const abandonOrder = (id, rowVersion) => request(`${baseUrl}/goodWills/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));
// 提交
export const submitOrder = (id, rowVersion) => request(`${baseUrl}/goodWills/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getSubmitDesc));

// 确认
export const confirmOrder = (id, data) => request(`${baseUrl}/goodWills/${id}/confirm`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getConfirmDesc));
// 车辆售后档案查询
export const getVin = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/vehicleSolds/forGoodWill?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getVinDesc));
};

//查询故障代码
export const faultItemQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/faultItems?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, faultItemQueryDesc));
};
//故障代码快捷查询
export const faultItemQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/faultItems/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, faultItemQueryDesc));
};
// 查询人员信息
export const getPersonnels = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/personnels/forDealerServiceInformation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPersonnelDesc));
};

